/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.modular.attendancerecord.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import reactor.core.Reactor;
import reactor.event.Event;
import vip.xiaonuo.core.annotion.BusinessLog;
import vip.xiaonuo.core.annotion.Permission;
import vip.xiaonuo.core.enums.LogAnnotionOpTypeEnum;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.pojo.response.ResponseData;
import vip.xiaonuo.core.pojo.response.SuccessResponseData;
import vip.xiaonuo.core.util.DateUtils;
import vip.xiaonuo.facedoor.modular.reactor.dto.MemberAttendanceVo;
import vip.xiaonuo.facedoor.modular.topic.EventTopic;
import vip.xiaonuo.modular.attendancegroup.entity.AttendanceGroup;
import vip.xiaonuo.modular.attendancerecord.mapper.AttendanceRecordMapper;
import vip.xiaonuo.modular.attendancerecord.param.*;
import vip.xiaonuo.modular.attendancerecord.service.AttendancePlanService;
import vip.xiaonuo.modular.attendancerecord.service.AttendancePlanService_OneDay;
import vip.xiaonuo.modular.attendancerecord.service.AttendanceRecordService;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import vip.xiaonuo.modular.attendancerecord.service.AttendanceRosteringService;
import vip.xiaonuo.modular.attendanceshiftgroup.service.AttendanceShiftGroupService;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 考勤记录控制器
 *
 * @author JOE
 * @date 2022-11-15 17:14:13
 */
@RestController
public class AttendanceRecordController {

    @Resource
    private AttendanceRecordService attendanceRecordService;

    @Resource
    private AttendancePlanService_OneDay onePlanService;

    @Resource
    private AttendancePlanService planService;

    @Resource
    private AttendanceRecordMapper mapper;

    @Resource
    private AttendanceShiftGroupService attendanceShiftGroupService;

    @Resource
    private AttendanceRosteringService rosteringService;

    @Autowired
    private Reactor reactor;

    /**
     * 查询考勤记录
     *
     * @author JOE
     * @date 2022-11-15 17:14:13
     */
    @Permission
    @GetMapping("/attendanceRecord/page")
    @BusinessLog(title = "考勤记录_查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData page(AttendanceRecordQueryParam param) {
        return new SuccessResponseData(attendanceRecordService.findPage(param));
    }

    /**
     * 添加考勤记录
     *
     * @author JOE
     * @date 2022-11-15 17:14:13
     */
    @Permission
    @PostMapping("/attendanceRecord/add")
    @BusinessLog(title = "考勤记录_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public ResponseData add(@RequestBody @Validated(AttendanceRecordParam.add.class) AttendanceRecordParam attendanceRecordParam) {
            attendanceRecordService.add(attendanceRecordParam);
        return new SuccessResponseData();
    }

    /**
     * 删除考勤记录
     *
     * @author JOE
     * @date 2022-11-15 17:14:13
     */
    @Permission
    @PostMapping("/attendanceRecord/delete")
    @BusinessLog(title = "考勤记录_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public ResponseData delete(@RequestBody @Validated(AttendanceRecordParam.delete.class) AttendanceRecordParam attendanceRecordParam) {
            attendanceRecordService.delete(attendanceRecordParam);
        return new SuccessResponseData();
    }

    /**
     * 编辑考勤记录
     *
     * @author JOE
     * @date 2022-11-15 17:14:13
     */
    @Permission
    @PostMapping("/attendanceRecord/edit")
    @BusinessLog(title = "考勤记录_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData edit(@RequestBody @Validated(AttendanceRecordParam.edit.class) AttendanceRecordParam attendanceRecordParam) {
            attendanceRecordService.edit(attendanceRecordParam);
        return new SuccessResponseData();
    }

    /**
     * 查看考勤记录
     *
     * @author JOE
     * @date 2022-11-15 17:14:13
     */
    @Permission
    @GetMapping("/attendanceRecord/detail")
    @BusinessLog(title = "考勤记录_查看", opType = LogAnnotionOpTypeEnum.DETAIL)
    public ResponseData detail(@Validated(AttendanceRecordParam.detail.class) AttendanceRecordParam attendanceRecordParam) {
        return new SuccessResponseData(attendanceRecordService.detail(attendanceRecordParam));
    }

    /**
     * 考勤记录列表
     *
     * @author JOE
     * @date 2022-11-15 17:14:13
     */
    @Permission
    @GetMapping("/attendanceRecord/list")
    @BusinessLog(title = "考勤记录_列表", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData list(AttendanceRecordParam attendanceRecordParam) {
        return new SuccessResponseData(attendanceRecordService.list(attendanceRecordParam));
    }

    /**
     * 批量生成考勤记录
     *
     * @author JOE
     * @date 2022-11-15 17:14:13
     */
    @Permission
    @PostMapping("/attendanceRecord/createPlan")
    @BusinessLog(title = "考勤记录_批量生成", opType = LogAnnotionOpTypeEnum.ADD)
    public ResponseData createPlan(@RequestBody AttendanceGroup group) {
        onePlanService.createPlan(group);
        return new SuccessResponseData();
    }

    /**
     * 批量生成考勤记录
     *
     * @author JOE
     * @date 2022-11-15 17:14:13
     */
    @Permission
    @PostMapping("/attendanceRecord/createFlexPlan")
    @BusinessLog(title = "考勤记录_批量生成弹性计划", opType = LogAnnotionOpTypeEnum.ADD)
    public ResponseData createFlexPlan(@RequestBody AttendanceGroup group) {
        planService.createPlan(group);
        return new SuccessResponseData();
    }

    @Permission
    @GetMapping("/attendanceRecord/summary")
    @BusinessLog(title = "考勤记录_数据汇总", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData summary(AttendanceRecordQueryParam param) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String endDate_str = param.getEndTime();
        String today_str = sdf.format(new Date()) + " 23:59:59";
        endDate_str = StringUtils.isEmpty(endDate_str) ? today_str : endDate_str;
        param.setEndTime(endDate_str);

        return new SuccessResponseData(attendanceRecordService.summary(param));
    }

    @Permission
    @PostMapping("/attendanceRecord/summaryExport")
    @BusinessLog(title = "考勤记录_汇总导出", opType = LogAnnotionOpTypeEnum.EXPORT)
    public void summaryExport(@RequestBody AttendanceRecordQueryParam param) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String endDate_str = param.getEndTime();
        String today_str = sdf.format(new Date()) + " 23:59:59";
        endDate_str = StringUtils.isEmpty(endDate_str) ? today_str : endDate_str;
        param.setEndTime(endDate_str);

        attendanceRecordService.export(param);
    }

    @Permission
    @PostMapping("/attendanceRecord/batchEdit")
    @BusinessLog(title = "考勤记录_批量修改", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData batchEdit(@RequestBody AttendanceRecordBatchEditParam param) {
        attendanceRecordService.batchUpdate(param);
        return new SuccessResponseData();
    }


    @GetMapping("/test/in")
    public ResponseData test(Long memberId,String time) {
        // 发布签到时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = sdf.parse(time);
            reactor.notify(EventTopic.MEMBER_ATTENDANCE,Event.wrap(new MemberAttendanceVo().setMemberId(memberId).setTime(date)));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return new SuccessResponseData();
    }

    @Permission
    @PostMapping("/attendanceRecord/rostering")
    @BusinessLog(title = "考勤记录_排班", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData rostering(@RequestBody AttendanceRecordRosteringParam param) {
        return new SuccessResponseData(rosteringService.rostering(param));
    }

    @Permission
    @PostMapping("/attendanceRecord/shifts")
    @BusinessLog(title = "考勤记录_排班班次", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData shifts(@RequestBody AttendanceRecordRosteringParam param) {
        return new SuccessResponseData(rosteringService.shifts(param));
    }

    @Permission
    @PostMapping("/attendanceRecord/doRoster")
    @BusinessLog(title = "考勤记录_排班班次", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData doRoster(@RequestBody AttendanceDoRosterParam param) {
        rosteringService.doRoster(param);
        return new SuccessResponseData();
    }


}
